/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kz.dasm.baing.rest;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import kz.dasm.baing.Authenticator;
import org.jboss.logging.Logger;
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.Headers;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

/**
 *
 * @author Kreedmart
 */

@Provider
@ServerInterceptor
public class AuthInterceptor implements PreProcessInterceptor {

  static final private Logger logger = Logger.getLogger(
          AuthInterceptor.class.getName());

  @Inject
  Authenticator auth;
  
  public ServerResponse preProcess(HttpRequest hr, ResourceMethod rm) 
          throws Failure, WebApplicationException {    
    
    if (!hr.getPreprocessedPath().startsWith("/auth/")) {
      if (!auth.isAuthorized()) {            
        return new ServerResponse("Unauthorized", 401, new Headers<Object>());
      }
    }
        
    return null;
  }  
  
}
